package org.wyn.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.wyn.mongodb.bean.Message;
import org.wyn.mongodb.bean.Message.Mee;
import org.wyn.mongodb.bean.UserDetail;
import org.wyn.mongodb.bean.VisitHistory;
import org.wyn.mongodb.dao.impl.MessageDao;
import org.wyn.mongodb.dao.impl.UserDetailDao;
import org.wyn.mongodb.dao.impl.VisitHistoryDao;
import org.wyn.service.IProfileTabService;

import com.google.inject.Inject;
/**
 * 个人主页Tab service实现层
 * @author wyn
 *
 */
public class ProfileTabServiceImpl implements IProfileTabService{

	@Override
	public List<String[]> getRecentVisitors(String userName) {
		try {
			VisitHistory vh = visitHistoryDao.findOne("userName",userName);
			List<String> visitors = vh.getRecentVisitors();
			List<String[]> result = new ArrayList<String[]>();
			for(String visitor : visitors){
				String[] array = new String[3];
				array[0] = visitor;//访问者userName
				UserDetail ud = userDetailDao.findOne("userName", visitor);
				array[1] = ud.getRealName();//访问者realName
				array[2] = ud.getPortrait().toString();//访问者头像
				result.add(array);
			}
			return result;
		} catch (Exception e) {
			return Collections.emptyList();
		}
	}
	@Override
	public UserDetail getUserDetailByUserName(String userName) {
		return userDetailDao.findOne("userName", userName);
	}
	@Override
	public boolean leaveMessage(String fromUser, String toUser, String content) {
		Message m = messageDao.findOne("userName", toUser);
		List<Mee> list = m.getMees();
		content = content.trim();
		UserDetail ud =  userDetailDao.findOne("userName", fromUser);
		list.add(0, new Mee(fromUser,ud.getRealName(),content,ud.getPortrait()));
		messageDao.save(m);
		return false;
	}
	@Override
	public List<Mee> viewMessageByToUser(String userName) {
		return messageDao.findOne("userName",userName).getMees();
	}
	@Inject
	private VisitHistoryDao visitHistoryDao;
	@Inject
	private UserDetailDao userDetailDao;
	@Inject
	private MessageDao messageDao;
	public VisitHistoryDao getVisitHistoryDao() {
		return visitHistoryDao;
	}
	public void setVisitHistoryDao(VisitHistoryDao visitHistoryDao) {
		this.visitHistoryDao = visitHistoryDao;
	}
	public UserDetailDao getUserDetailDao() {
		return userDetailDao;
	}
	public void setUserDetailDao(UserDetailDao userDetailDao) {
		this.userDetailDao = userDetailDao;
	}
	public MessageDao getMessageDao() {
		return messageDao;
	}
	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}
	
}
